package com.zjj.spring.read.xml.life;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
/**
 * ApplicationContext生命周期
 * <pre>
 *  启动容器
 *  调用BeanFactoryPostProcessor的postPorcessBeanFactory()对工厂定义信息进行后处理
 *  通过getBean()调用一个bean
 *  调用InstantiationAwareBeanPostProcessor的postPorcessBeforeInstiation()方法
 *  实例化
 *  调用InstantiationAwareBeanPostProcessor的postPorcessAfterInstiation()方法
 *  调用InstantiationAwareBeanPostProcessor的postPorcessPropertyValues()方法
 *  设置属性值
 *  调用BeanNameAware的setBeanName()方法
 *  调用BeanFactoryAware的setBeanFactory()方法
 *  调用ApplicationContextAware的setApplicationContext()方法
 *  调用BeanPostProcessor的postPorcessBeforeInitialization()方法	- 针对所有bean处理
 *  调用InitializingBean的afterPropertiesSet()方法
 *  通过init-method设置的初始化方法
 *  调用BeanPostProcessor的postProcessAfterInitialization()方法	- 针对所有bean处理
 *  singletion           vs         prototype
 *  - Spring缓存池中准备就绪的bean		-将准备就绪的bean交给调用者
 *  容器销毁
 *  调用DisposableBean的destroy()方法
 *  调用destroy-method方法
 * </pre>
 * @author zhuangjiajie778
 */
public class LifecycleBean implements InitializingBean, DisposableBean {

	/**InitializingBean - 功能和init-method类似，但是此方式需继承spring的类，和spring 耦合，不建议使用*/
	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("【4】----------------Enter AwareBean.afterPropertiesSet() from InitializingBean---------------");
		System.out.println("【4】AwareBean.afterPropertiesSet-------");
		System.out.println("【4】----------------Enter AwareBean.afterPropertiesSet() from InitializingBean---------------");
		System.out.println();
	}
	/**init-method - 容器启动自动触发*/
	public void beanInit () {
		System.out.println("【5】----------------Enter LifecycleBean.beanInit() from init-method ---------------");
		System.out.println("【5】LifecycleBean.beanInit-------");
		System.out.println("【5】----------------Enter LifecycleBean.beanInit() from init-method ---------------");
		System.out.println();
	}
	/** - 调用AbstractApplicationContext#destroy触发
	 *  - 功能和destroy-method类似，但是此方式需继承spring的类，和spring 耦合，不建议使用*/
	@Override
	public void destroy() throws Exception {
		System.out.println("【6】----------------Enter LifecycleBean.destroy() from DisposableBean ---------------");
		System.out.println("【6】LifecycleBean.destroy-------");
		System.out.println("【6】----------------Exit  LifecycleBean.destroy() from DisposableBean ---------------");
		System.out.println();
	}
	/**destroy-method - 调用AbstractApplicationContext#destroy触发*/
	public void beanDestroy () {
		System.out.println("【7】----------------Enter LifecycleBean.beanDestroy() from destroy-method ---------------");
		System.out.println("【7】LifecycleBean.beanDestroy-------");
		System.out.println("【7】----------------Enter LifecycleBean.beanDestroy() from destroy-method ---------------");
		System.out.println();
	}

}
